<template>
    <div class="top">
        <div class="choose">
                 <span>
                  描述：<input type="text" class="search_child" placeholder="如:发布人/内容" v-model="searchValue"></span>
            <span>
                  发表时间：<input type="date" class="search_child" v-model="annoPage.dateTime"></span>
            <span>
                  公告标题：<input type="text" class="search_child" v-model="annoPage.title" placeholder="文章标题"></span>
            <span  class="search_click" @click="search_">搜索</span>
        </div>

        <span class="add" @click="anno">
             十 &nbsp;新增
        </span>
    </div>
    <div class="bottom">
        <table cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th>序号</th>
                <th>ID</th>
                <th style="width: 120px">发布人</th>
                <th style="width: 120px">标题</th>
                <th style="min-width: 150px">发布日期</th>
                <th class="content_">发布内容</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,index) in fuzzyQueries" v-if="fuzzyQueries.length>0">
                <td style="width: 50px">{{ total.current > 1 ? (index+1)+14*(total.current-1) : index+1}}</td>
                <td >{{ item.id }}</td>
                <td v-html="highlight_(item.publisher )" class="overflow"></td>
                <td v-html="item.title" class="overflow"></td>
                <td style="width: 150px">{{ item.releaseTime }}</td>
                <td class="content overflow" v-html="highlight_(item.content )"></td>
                <td style="width: 80px">
                          <span class="button_style"
                                :style="{'background': item.statue === 1 ? '#32CD32':'#DC143C' }">
                              {{ item.statue === 1 ? '正常' : '禁用' }}
                          </span>
                </td>
                <td style="width: 80px">
                    <span class="edit" @click="del_(item,index)">删除</span>
                </td>
            </tr>
            <tr v-else-if="fuzzyQueries.length===0 && load_" style="height: 50vh;background: transparent">
                <LoadTheAnimation ></LoadTheAnimation>
            </tr>
            <tr v-show="!load_">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td><h2>未查询到任何数据</h2></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="bottom_">
        <span style="margin-right:10px">共{{ total.page }}页, 共{{ total.sum }}条数据 当前第{{ total.current }}页</span>
        <span class="jian" @click="left_">
           <img src="../../../public/static/jian_r.png" alt="">
       </span>
        <input type="text" class="limit" v-model="total.current" @focusout="limit_">
        <span class="jian" @click="right_">
           <img src="../../../public/static/jian_l.png" alt="">
       </span>
        <select @change ="page_($event)" class="page_index">
            <option  v-for="(item,index) in 3" >{{(index+1)*10+4}}/条</option>
        </select>
    </div>


    <PromptWindow v-show="box_confirm" @confirm="confirm" :info="info" :index="index"></PromptWindow>
    <AnnouncementEdit v-show="eadit" @handleCommentSend="handleCommentSend"></AnnouncementEdit>
</template>

<script>
import api from "@/api";
import AnnouncementEdit from "@/components/announcement/AnnouncementEdit.vue";
import PromptWindow from "@/components/publicCommon/PromptWindow.vue";
import LoadTheAnimation from "@/components/publicCommon/LoadTheAnimation.vue";

export default {
    name: "Announcement",
    components: {LoadTheAnimation, PromptWindow, AnnouncementEdit},
    data() {
        return {
            select: false,
            searchValue: "",
            fuzzyQueries: [],
            annoPage: {
                index: 1,
                condition: "",
                title:null,
                dateTime:null,
                page:14,
            },
            total: {
                sum: '',
                page: '',
                size: '',
                current: '',
            },
            eadit: false,
            info: "",
            box_confirm: false,
            index: "",
            del_info: false,
            item_added: false,
            load_:true,
        }
    },
    mounted() {
        this.request_(this.annoPage);
    },
    watch: {
        'annoPage.page'(newValue,oldValue){
            this.annoPage.page = newValue;
            this.request_(this.annoPage);
        },
        'annoPage.dateTime'(newValue,oldValue){
            if(newValue == "") {
                this.annoPage.dateTime = null
            }
        },
        del_info(newValue, oldValue) {
            let _this = this;
            if (newValue) {
                api.deleteAdminAnnouncement(this.info).then((res) => {
                    if (res.data.code === 200) {
                        if (_this.annoPage.index === 0) {
                            this.$store.commit("sys_announcement", this.fuzzyQueries[1].content);
                        }
                        this.request_(this.annoPage);
                        this.$store.commit("requestInfo", "删除成功!");
                        alert("")
                    } else {
                        this.$store.commit("requestInfo", "删除失败!");
                    }
                })
                this.del_info = false;
            }
        },
        'total.current'(newVal, oldVal) {
            let reg = /^\+?[1-9]\d*$/;
            if(reg.test(newVal)){
                this.annoPage.index = newVal;
            }
            if(newVal > this.total.page){
                this.annoPage.index = 1;
            }
        },
        searchValue(newValue, oldValue) {
            if (newValue === "") {
                this.annoPage.condition = null;
                this.request_(this.annoPage);
            }
            this.annoPage.condition = newValue
        },
    },
    methods: {
        page_(event){
            this.annoPage.page = parseInt(event.target.value);
        },
        handleCommentSend() {
            this.annoPage.index = 1;
            this.annoPage.condition = null;
            setTimeout(() => {
                this.request_(this.annoPage);
            }, 200)
            this.eadit = false;
        },
        anno() {
            this.eadit = true;
            this.request_(this.annoPage);
        },

        confirm(index, bool) {
            this.box_confirm = false;
            if (bool) {
                this.del_info = true;
                this.fuzzyQueries.splice(index, 1);
            }
        },

        del_(info, index) {
            this.info = info;
            this.index = index;
            this.box_confirm = true;

        },
        search_() {
            this.request_(this.annoPage);
        },
        highlight_(text) {
            text = text + "";
            if (this.searchValue !== "")
                return text.replace(this.searchValue, '<span style="background: #f6f604;">' + this.searchValue + '</span>')
            else return text
        },
        limit_() {
            this.request_(this.annoPage);
        },
        request_(page) {
            api.getAdminAnnouncement(page).then((res) => {
                this.fuzzyQueries = res.data.data.records;
                this.total.sum = res.data.data.total;
                this.total.page = res.data.data.pages;
                this.total.current = res.data.data.current;
                this.total.size = res.data.data.size;
                if(this.fuzzyQueries.length===0){
                    setTimeout(()=>{
                        this.load_ = false;
                    },1000)
                }else {
                    this.load_ = true;
                }
            })
        },
        left_() {
            this.annoPage.index -= 1;
            if (this.annoPage.index < 1) {
                this.annoPage.index = 1;
                this.$store.commit("requestInfo", "已是第一页!");
                return;
            }
            this.request_(this.annoPage);
        },
        right_() {
            this.annoPage.index += 1;
            if (this.annoPage.index > this.total.page) {
                this.annoPage.index = this.total.page;
                this.$store.commit("requestInfo", "已是最后一页!");
                return;
            }
            this.request_(this.annoPage);
        }
    }
}
</script>

<style scoped>
.add {
    padding: 5px 6px;
    color: white;
    background: #3586f1;
    border-radius: 3px;
    font-size: 14px;
    cursor: pointer;
}

.add:hover {
    opacity: .9;
}


@media (min-width: 300px) and (max-width: 600px) {
    th {
        min-width: 100px;
    }
}

.search_child{
    height: 30px;
    padding:0 10px;
    outline:none;
}
.choose{
    margin-left: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.choose span{
    margin-right: 10px;
}
.search_click{
    display: inline-block;
    background: #057cf8;
    padding: 4px 7px;
    color: white;
    cursor: pointer;
}
.search_click:hover{
    opacity: .8;
}
.page_index{
    height: 26px;
    outline: none;
    padding: 0 7px;
    color: #4297ec;
    border: 1px solid #282828;
}
option{
    padding: 10px;
}
</style>














